package web;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Iterator;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;

import model.FitsFile;
import model.FitsFileList;
import dao.FitsDao;

@SuppressWarnings("serial")
public class SearchServlet extends HttpServlet {
	
	private static Logger logger = Logger.getLogger(SearchServlet.class);

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

    	BasicConfigurator.configure();
		// want a list of the surveys to get data for
		String[] surveyNames = null;
		Enumeration paramNames = request.getParameterNames();
		while (paramNames.hasMoreElements()) {
			String paramName = (String) paramNames.nextElement();
			if (paramName.equals("survey")) { surveyNames = request.getParameterValues(paramName); }
		}
//		for (int i = 0; i < surveyNames.length; i++) {
//			System.out.println(surveyNames[i]);
//		}

		Double RA1=-9999.0; Double RA2=-9999.0; Double DEC1=-9999.0; Double DEC2=-9999.0;
		
		
		logger.info("ra1 from interface: "+request.getParameter("ra1"));
		if(!request.getParameter("ra1").equals("")){  RA1 = Double.parseDouble(request.getParameter("ra1")); }
		logger.info("ra2 from interface: "+request.getParameter("ra2"));
		if(!request.getParameter("ra1").equals("")){  RA2 = Double.parseDouble(request.getParameter("ra2")); }
		logger.info("dec1 from interface: "+request.getParameter("dec1"));
		if(!request.getParameter("ra1").equals("")){  DEC1 = Double.parseDouble(request.getParameter("dec1")); }
		logger.info("dec2 from interface: "+request.getParameter("dec2"));
		if(!request.getParameter("ra1").equals("")){  DEC2 = Double.parseDouble(request.getParameter("dec2")); }
		
		logger.info("ra1"+RA1);
		logger.info("ra2"+RA2);
		logger.info("dec1"+DEC1);
		logger.info("dec2"+DEC2);
		boolean isRestricted = false;
		if((!RA1.equals(-9999.0))&&(!RA2.equals(-9999.0))&&(!DEC1.equals(-9999.0))&&(!DEC2.equals(-9999.0))){
			isRestricted=true;
		}
		
		logger.info("Is restricted: "+isRestricted);
		
		ArrayList<String> surveys = new ArrayList<String>(Arrays.asList(surveyNames));

		FitsFileList ffList = new FitsFileList();
		FitsDao fd = new FitsDao();
		try {
			if(isRestricted==true){
				ffList = fd.getFilesFromDB(surveys,RA1,RA2,DEC1,DEC2);
			}
			else{
				ffList = fd.getFilesFromDB(surveys);				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		ArrayList<FitsFile> alFFs = new ArrayList<FitsFile>();
		alFFs = ffList.getListOfFF();
		Iterator<FitsFile> itr = alFFs.iterator();
		
		if(itr.hasNext()){
		while (itr.hasNext()) {
			FitsFile ff = itr.next();
			System.out.println(ff.getFilename() + "  " + ff.getFolder() + "  " + ff.getAuthor() + "  " + ff.getTelescope() + "  "+ ff.getCtype1() + "  " + ff.getCtype2() + "  "+ ff.getCrval1() + "  " + ff.getCrval2());
		}
		}
		else{ logger.info("There are no fits files with these criteria");}
		
		request.setAttribute("alFFs", alFFs); // alFFs = the array list of Fits Files to be displayed
		getServletContext().getRequestDispatcher("/jsp/results.jsp").forward(request, response);
	}
}